package com.springcloud.setting.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.feign.client.DeviceTypeService;
import com.service.base.ResponseResult;
import com.service.bean.DeviceType;


@Controller
@RequestMapping("/devicetype")
public class DeviceTypeController {
	
	@Autowired
    private DeviceTypeService deviceTypeService;
	
	
	//to index link
	@RequestMapping(value = "/index" )
	public String index() {
		return "/devicetype/index";
	}
	
	@ResponseBody
    @RequestMapping(value = "/getList", method = RequestMethod.GET)
    public ResponseResult getList(DeviceType deviceType,
								  @RequestParam(defaultValue = "1") Integer pageNumber,
								  @RequestParam(defaultValue = "10") Integer pageSize){
		System.out.println("-------------11111111111111");
//        PageInfo<DeviceType> deviceTypePage = deviceTypeService.getPage(deviceType, pageNumber, pageSize);
        
//        System.out.println(deviceTypePage.getList().size());
        return deviceTypeService.getPage(deviceType, pageNumber, pageSize);
    }
	
	
	@RequestMapping(value = "/add" )
	public String toAdd(ModelMap map) {
		
		return "/devicetype/add";
	}
	
    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public ResponseResult createDeviceType(DeviceType deviceType){
    	
    	System.out.println("create devicetype");
        deviceTypeService.insert(deviceType);
        return ResponseResult.ok(deviceType.getId());
    }
    
    
    @ResponseBody
    @RequestMapping(value = "/delete/{deviceTypeId}", method = RequestMethod.POST)
    public ResponseResult delete(@PathVariable Integer deviceTypeId){
    	
    	System.out.println("deviceTypeId+++++++:::::" + deviceTypeId);
    	
//    	int count = this.menuService.getRoleMenuCountByMenuId(menuId);
//    	if (count > 0){
//			return ResponseResult.error(Codes.ROLE_IS_USED);
//		}
    	DeviceType deviceType = new DeviceType();
    	deviceType.setIsdelete("1");
    	deviceType.setId(new Long(deviceTypeId));
    	int count = deviceTypeService.delete(deviceType);
    
        return ResponseResult.ok(count);
    }
    
    @RequestMapping(value = "/edit/{deviceTypeId}" )
	public String toEdit(@PathVariable Integer deviceTypeId, ModelMap map) {
    	DeviceType deviceType = this.deviceTypeService.getDeviceType(deviceTypeId);
    	
    	System.out.println("deviceType--------:::::"+ deviceType.getCode());
		map.addAttribute("deviceType", deviceType);
		return "/deviceType/edit";
	}
    
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public ResponseResult update(DeviceType deviceType){
    	
        int count = deviceTypeService.update(deviceType);
        return ResponseResult.ok(count);
    }
}
